<?php
/**
 * 商家产品管理
 * @author Administrator
 *
 */
class shopProductManage extends CControllerManageBase{
	public function show()
	{
		$model = new shopProductModel();
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		$shop_name = $this->getRequestData("txt_shop_name","get");
		
		if($_POST)
		{
			$this->updateSortNum($model);
		}
		
		$urlParam = array();
		$where = "t.del_flag=0 and sp.del_flag=0 and t.shop_id=sp.shop_id";

		if($shop_name)
		{
			$where .= " and (shop_name like '%".$shop_name."%' or shop_simple_name like '%".$shop_name."%')";
			
			$urlParam["txt_shop_name"] = $shop_name;
		}
		
		$this->setViewParam("shop_name", $shop_name);
		
		$pType = Config::get("shop_product_type");
		
		$pageUrl = $this->getUrlParam($urlParam, array("txt_shop_name"));
		$pageUrl = $pageUrl? "?".$pageUrl:"";
		
		$list = $model->selectPage($pageIndex,$pageSize,"sp.*,t.shop_simple_name",$where,"sp.is_top desc,sp.sort_num desc,sp.p_id desc","",$c,"t_shop t,t_shop_product sp");
		$pageStr = CPage::showPage($pageIndex,$pageSize,$c,$pageUrl);
		$pageUrl = $pageUrl? "&page=".$pageIndex:"page=".$pageIndex;
		$this->setMenu("商家产品列表");
		$this->render("shopProduct/list",array("list"=>$list,"pType"=>$pType,"pageUrl"=>$pageUrl,"pageStr"=>$pageStr));
	}
	
	private function getPostInfo()
	{
		$modelInfo = array();
		if(!isset($_GET["id"]))
		{
			$modelInfo["user_name"] = $this->getRequestData("txt_user_name");
			$modelInfo["create_time"] = date("Y-m-d H:i:s");
		}
		$modelInfo["shop_name"] = $this->getRequestData("txt_shop_name");
		$modelInfo["phone"] = $this->getRequestData("txt_phone");
		$modelInfo["shop_simple_name"] = $this->getRequestData("txt_shop_simple_name");
		$modelInfo["back_account"] = $this->getRequestData("txt_back_account");
		$modelInfo["contact"] = $this->getRequestData("txt_contact");
		$modelInfo["mail"] = $this->getRequestData("txt_mail");
		$modelInfo["qq"] = $this->getRequestData("txt_qq");
		$modelInfo["bus_content"] = $this->getRequestData("txt_bus_content");
		$modelInfo["remark"] = $this->getRequestData("txt_remark");
		$modelInfo["bus_license_pic"] = $this->getRequestData("hidImgPath");
		
		return $modelInfo;
	}
	
	public function add(){
		$errorStr="";
		$rule=CCustomFormValidator::shopValidator();
		$this->formValidator->init(array("formID"=>"t_shopForm"));
		$this->formValidator->setRules($rule);
		if($_POST){
			$modelInfo = $this->getPostInfo();
			
			$modelInfo["user_pwd"] = CCommon::pwdMd5("000000");
			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{
				$model=new shopModel();
				$model->db->beginTransaction();
				$sid=$model->insert($modelInfo);

				if($sid>0)
				{
					$clist = $this->getRequestData("hidSelCommunityID"); //管理的社区
			        
					if($clist)
					{
						$clist = explode(",", $clist);
						foreach($clist as $cid)
						{
							$model->addShopCommunity($sid, $cid);
						}
					}
					
					$scInfo["shop_id"] = $sid;
					$scInfo["p_name"] = $modelInfo["shop_name"];
					$scInfo["pic_path"] = "/images/o_list_demo.jpg";
					$scInfo["p_type"] = 1;
					$scInfo["create_time"] = date("Y-m-d H:i:s");
					
					$scModel = new shopProductModel();
					$scModel->insert($scInfo);
					$model->db->commit();
					
					$this->sysMsg("新增成功",0,array(array("text"=>"返回商家列表","href"=>$this->_manageDomain."shopManage/show/"),array("text"=>"继续新增","href"=>$this->_manageDomain."shopManage/add/")));
				}
				else
				{
					$this->sysMsg("新增失败",1);
				}
			}
		}

		$this->setJsBlock("init('','','',1);_changeSelCallBack=true;_mutiCommunity=true;");
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		$this->setMenu(array(array("text"=>"商家列表","href"=>$this->_manageDomain."shopManage/show/"),array("text"=>"商家新增")));
		$this->render("shop/add",array("error"=>$errorStr));
	}

	public function edit(){
		$p_id = $this->getRequestData("id","GET");
		$errorStr = "";

		$rule=CCustomFormValidator::shopProductValidator();
		$this->formValidator->init(array("formID"=>"shopProductForm"));
		$this->formValidator->setRules($rule);

		$model=new shopProductModel();
		if($_POST){

			$error = $this->formValidator->run();
			if($error){
				$errorStr = "<div><ul>";
				foreach ($error as $item){
					$errorStr .= "<li>".$item."</li>";
				}
				$errorStr .= "</ul></div>";
			}
			else{

				$modelInfo["p_id"] = $p_id;
				$modelInfo["p_name"] = $this->getRequestData("txt_p_name");
				$modelInfo["p_type"] = $this->getRequestData("sel_p_type");
				$modelInfo["pic_path"] = $this->getRequestData("hidImgPath");
				$model->updateByPkId($modelInfo);
				
		        header("location:".$this->_manageDomain."shopProductManage/show/");
		        exit();
			}
		}
		else{
			$modelInfo = $model->selectByWhere(
				"t_shop s, t_shop_product sp",
				"sp.*,s.shop_name",array(),"s.shop_id=sp.shop_id and s.del_flag=0 and sp.del_flag=0 and p_id=".$p_id);
			if(!$modelInfo) {
				$this->sysMsg("记录不存在",1);
			}
		}
		$pType=Config::get("shop_product_type");
		
		$this->setJsBlock("init('','','',1);_changeSelCallBack=true;_mutiCommunity=true;");
		$this->setStyleFile("/css/validator.css");
		$this->setJsFile(array("/js/formValidator.js","/js/formValidatorRegex.js"));
		$this->setMenu(array(array("text"=>"商家列表","href"=>$this->_manageDomain."shopManage/show/"),array("text"=>"商家修改")));
		$this->render("shopProduct/edit",array("error"=>$errorStr,"modelInfo"=>$modelInfo,"pType"=>$pType));
	}
	
	public function setTop()
	{
		$id = $this->getRequestData("id","GET");
		$type = $this->getRequestData("istop","GET");
		if ($id)
        {
            $model = new shopProductModel();
            
			$model->setTop($id,$type);
			
            header("location:".$this->_manageDomain."shopProductManage/show");
            exit;
        }
	}
}